package com.tms.app.tms.ui.base.presenter;


import com.google.gson.Gson;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.app.MyApp;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.utils.NetUtils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.util.KeyValue;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

/**
 * Created by hl on 2017/6/9.
 */

public abstract class AbsServicexUtils
        implements org.xutils.common.Callback.CommonCallback<String>{
    public Gson gson=new Gson();
    private int hideLoading=0;
    public static final String showAndHideLoading="1";
    public static final String onlyShowLoading="2";
    public static final String onlyHideLoading="3";
    public static final String noShowAndHideLoading="4";


    /*********
     *  发送通知到View
     * @param e
     * @param json
     * @param <E>
     */
    public  <E > void sendMsgToView(Class<E> e, String json){
        E baseResponse=gson.fromJson(json, e);
        sendMsgToView(baseResponse);
    }
    public  <E > void sendMsgToView(E e){
        if (e!=null)
            EventBus.getDefault().post(e);
    }



    public void getObjectFromServer(RequestParams params){
        getObjectFromServer(params,0);
    }
    public void getObjectFromServer(RequestParams params,int post){
        getObjectFromServer(params,post,false);
    }

    /******
     * 发送请求
     * @param params
     * @param post
     * @param isloading
     */
    public void getObjectFromServer(RequestParams params,int post,boolean isloading){
         if (!NetUtils.isNetworkAvailable()){
             BaseResponse baseResponse=new BaseResponse();
             baseResponse.setCode(404);
             sendMsgToView(baseResponse);
             return;
         }


        System.out.println("请求地址："+params.getUri());
        for (KeyValue keyValue : params.getQueryStringParams()) {
            System.out.println("请求参数"+keyValue.key+":"+keyValue.getValueStr());
            if (keyValue.key.equals("showLoading")){
//                if (keyValue.getValueStr().equals("2017"))// 上报位置
//                {
//                    this.hideLoading=2017;
//                }else {
//                    this.hideLoading=0;
//                }
//                MyApp.loadingMap.put(params.getUri(),1);
//                if (keyValue.getValueStr().equals(showAndHideLoading))////1 开启loading 并结束 2开启不结束，3不开启但结束
//                {
//                    BaseResponse baseResponse=new BaseResponse();
//                    baseResponse.setCode(3001);
//                    sendMsgToView(baseResponse);
//                    this.hideLoading=1;
//                }
//                if (keyValue.getValueStr().equals(onlyShowLoading))////1 开启loading 并结束 2开启不结束，3不开启但结束
//                {
//                    BaseResponse baseResponse=new BaseResponse();
//                    baseResponse.setCode(3001);
//                    sendMsgToView(baseResponse);
//                    this.hideLoading=0;
//                }
//                if (keyValue.getValueStr().equals(onlyHideLoading))////1 开启loading 并结束 2开启不结束，3不开启但结束
//                {
//                    this.hideLoading=1;
//                }
//                if (keyValue.getValueStr().equals(noShowAndHideLoading))////1 如果未开启则开启，
//                {
//
//                    this.hideLoading=0;
//                }
            }else {
                hideLoading=0;
            }
        }
        for (KeyValue keyValue : params.getBodyParams()) {
            System.out.println("请求参数"+keyValue.key+":"+keyValue.getValueStr());
        }




//        params.setAsJsonContent(true);
//        params.setBodyContent(js_request.toString());
       // params.setCacheMaxAge(0);
        params.setConnectTimeout(3000);
        params.addBodyParameter("api_version", Constant.API_VERSION);
        if (post==1)
        {
            x.http().post(params,this);
        }else {
            x.http().get(params,this);
        }

            System.out.println("请求地址："+params.getUri());
            for (KeyValue keyValue : params.getQueryStringParams()) {
                System.out.println("请求参数"+keyValue.key+":"+keyValue.getValueStr());
            }
            for (KeyValue keyValue : params.getBodyParams()) {
                System.out.println("请求参数"+keyValue.key+":"+keyValue.getValueStr());
            }

    }




    @Override
    public void onSuccess(String result) {
            System.out.println("返回结果"+result);
    }
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
            ex.printStackTrace();
        if (ex instanceof SocketTimeoutException){
            if (hideLoading==2017){
                return;
            }
            BaseResponse baseResponse=new BaseResponse();
            baseResponse.setCode(6001);
            sendMsgToView(baseResponse);
        }
        if (ex instanceof ConnectException){
            BaseResponse baseResponse=new BaseResponse();
            baseResponse.setCode(6002);
            sendMsgToView(baseResponse);
        }

    }

    @Override
    public void onCancelled(CancelledException cex) {

    }

    @Override
    public void onFinished() {
        if (hideLoading==1){
            BaseResponse baseResponse=new BaseResponse();
            baseResponse.setCode(5001);
            sendMsgToView(baseResponse);
        }

    }





}
